<?php

declare(strict_types=1);

namespace app\common\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class UserConcern extends BaseModel
{

    public function expert(){
        return $this->belongsTo(Expert::class,'expert_id');
    }

    //入库后
    public static function onAfterInsert($model)
    {
        $expert = new Expert();
        $row = $expert->find($model->expert_id);
        if ($row) {
            $row->followers += PublicStatus::PUBLIC_STATUS_ONE;
            $row->save();
        }
        return true;
    }

    //删除后
    public static function onAfterDelete($model)
    {

        $expert = new Expert();
        $row = $expert->find($model->expert_id);

        if ($row) {
            $row->followers -= PublicStatus::PUBLIC_STATUS_ONE;
            $row->save();
        }
        return true;
    }
    //关注
    public static function ConcernClass($expert_id, $user_id)
    {
        $row = self::where(['expert_id' => $expert_id, 'user_id' => $user_id])->find();
        if (!$row) {
            return self::create(['expert_id' => $expert_id, 'user_id' => $user_id]);
        } else {
            return $row->delete(); //取消
        }
    }
}
